//
//  ZYXHomeViewController.m
//  RuiBeiKang
//
//  Created by iOS开发1 on 2019/1/2.
//  Copyright © 2019年 JW. All rights reserved.
//

#import "ZYXHomeViewController.h"
#import "ZYXHomeModel.h"
#import "ZYXHomeHeaderCell.h"
#import "ZYXHomeOneCell.h"
#import "ZYXHomeTwoCell.h"
#import "ZYXHomeThreeCell.h"
#import "ZYXHomeTwoHeaderView.h"
#import "ZYXHomeTwoFooter.h"
#import "ZYXThreeHeaderView.h"

#import "ZYXHomeHeaderView.h"

#import "ZYXHighQualityVC.h"
#import "ZYXStudyVC.h"

#import "ZYXSearchVC.h"
#import "ZYXClassVC.h"
#import "ZYXStudyDetailVC.h"
#import "ZYXVideoPlayVC.h"
#import "WebVC.h"
@interface ZYXHomeViewController ()<ZYXHomeHeaderCellDelegate>
@property (strong,nonatomic) NSMutableArray *oneArr;

@property (strong,nonatomic) NSMutableArray *dataArr;

@property (strong,nonatomic) ZYXHomeModel *allModel;
@property (strong,nonatomic)ZYXHomeHeaderView *nav;
@end

@implementation ZYXHomeViewController
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
}
#pragma mark NAV
-(void)setupNAV{
    self.nav = [[NSBundle mainBundle]loadNibNamed:@"ZYXHomeHeaderView" owner:nil options:nil].lastObject;
    [self.nav.sraechBtn addTarget:self action:@selector(clickSearchBtn) forControlEvents:UIControlEventTouchUpInside];
    self.nav.searchImg = IMG(@"sousuo");
    self.nav.searchField.placeholder = @"计算机基础课程";
    [self.view addSubview:self.nav];
    [self.nav mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(0);
        make.left.offset(0);
        make.right.offset(0);
        make.height.offset(NavigationBar_Height);
    }];
}
///进入搜搜
-(void)clickSearchBtn{
    NSLog(@"点击了搜搜");
    ZYXSearchVC *vc = [[ZYXSearchVC alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark MainUI
-(void)setupViews{
    [self setupNAV];
    
    UICollectionViewFlowLayout *fl = [[UICollectionViewFlowLayout alloc]init];
    fl.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, NavigationBar_Height, SCREEN_WIDTH, SCREEN_HEIGHT-NavigationBar_Height-TabBar_Height) collectionViewLayout:fl];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.scrollEnabled = YES;
    self.collectionView.showsVerticalScrollIndicator = NO;
    self.collectionView.backgroundColor = [UIColor whiteColor];
        [self.collectionView registerNib:[UINib nibWithNibName:@"ZYXHomeHeaderCell" bundle:nil] forCellWithReuseIdentifier:@"header"];
    [self.collectionView registerNib:[UINib nibWithNibName:@"ZYXHomeOneCell" bundle:nil] forCellWithReuseIdentifier:@"one"];

    [self.collectionView registerNib:[UINib nibWithNibName:@"ZYXHomeTwoCell" bundle:nil] forCellWithReuseIdentifier:@"two"];
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"ZYXHomeThreeCell" bundle:nil] forCellWithReuseIdentifier:@"three"];
    [self.collectionView registerNib:[UINib nibWithNibName:@"ZYXHomeTwoHeaderView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"twoHeader"];
      [self.collectionView registerNib:[UINib nibWithNibName:@"ZYXThreeHeaderView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"threeHeader"];
  
    [self.collectionView registerNib:[UINib nibWithNibName:@"ZYXHomeTwoFooter" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"twoFooter"];
    [self setupRefresh];
    [self.view addSubview:self.collectionView];
}
#pragma mark 加载数据
- (void)setupRefresh {
    
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{

        [self loadData];
    
    }];
//    self.collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
//        [self endRefresh];
//    }];
 
}
- (void)endRefresh {
    [self.collectionView.mj_footer endRefreshing];
    [self.collectionView.mj_header endRefreshing];
}
-(void)loadData{
    [NetWork POSTWithUrlString:kInterfaceAllUrl(PATH_IndexIndex) parmas:nil successBloock:^(NSDictionary *responseObject) {
        NSLog(@"responseObject=%@",responseObject);
        if ([responseObject[@"code"] isEqualToNumber:@200]) {
            self.allModel = [ZYXHomeModel mj_objectWithKeyValues:responseObject];

           
        }else{
            [JKToast showWithText:responseObject[@"message"]];
        }
        [self.collectionView reloadData];
        [self endRefresh];
    } failureBlock:^(NSError *error) {
         [self endRefresh];
    }];
}
#pragma mark collectionDelegate
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 5;
    
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (section==0) {
        return 1;
    }else if (section==1) {
        return self.allModel.fenlei.count;
    }else if (section==2) {
        return self.allModel.xin.count;
    }else if (section==3) {
        return self.allModel.tuijian.count;
    }else{
        return self.allModel.news.count;
    }
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        ZYXHomeHeaderCell *header = [collectionView dequeueReusableCellWithReuseIdentifier:@"header" forIndexPath:indexPath];
        header.delegate = self;
        header.imgArr  = self.allModel.banner_list;
        return header;
        
    }  else if (indexPath.section==1) {
     //
        ZYXHomeComModel *model = self.allModel.fenlei[indexPath.row];
        ZYXHomeOneCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"one" forIndexPath:indexPath];
        cell.model = model;
        return cell;
    }else if (indexPath.section==2) {
        //
        ZYXHomeComModel *model = self.allModel.xin[indexPath.row];
        ZYXHomeTwoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"two" forIndexPath:indexPath];
        cell.model = model;
        return cell;
    }else if (indexPath.section==3) {
        //
        ZYXHomeComModel *model = self.allModel.tuijian[indexPath.row];
        ZYXHomeTwoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"two" forIndexPath:indexPath];
        cell.model = model;
        return cell;
    }else{
        //
        ZYXHomeComModel *model = self.allModel.news[indexPath.row];
        ZYXHomeThreeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"three" forIndexPath:indexPath];
        cell.model = model;
        return cell;
    }

}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return CGSizeMake(SCREEN_WIDTH, HEIGHT(135));
    } else if (indexPath.section==1){
        CGFloat OneCellW = SCREEN_WIDTH/4.0f;
        CGFloat OneCellH = HEIGHT(100);
        return CGSizeMake(OneCellW, OneCellH);
    }else if (indexPath.section==4){
        CGFloat ThreeCellW = SCREEN_WIDTH;
        CGFloat ThreeCellH = HEIGHT(85);
        return CGSizeMake(ThreeCellW,ThreeCellH);
    }else{
        CGFloat TwoCellW = (SCREEN_WIDTH -HEIGHT(40))/3.0f;
        CGFloat TwoCellH = HEIGHT(115);
        return CGSizeMake(TwoCellW, TwoCellH);
    }
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
        if (section ==2 ||section ==3) {
            return UIEdgeInsetsMake(0, HEIGHT(10), 0, HEIGHT(10));
        }else{
            return UIEdgeInsetsMake(0, 0, 0, 0);
        }
    
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{

    return 0;
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{

    return 0;
}
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        if (indexPath.section == 2) {
            ZYXHomeTwoHeaderView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"twoHeader" forIndexPath:indexPath];
            view.imgView.image = [UIImage imageNamed:@"zuixinshangjia-1"];
            view.titleLabel.text = @"最新上架";
            view.moreBtn.tag = 1000;
            [view.moreBtn addTarget:self action:@selector(clickMoreHigh:) forControlEvents:UIControlEventTouchUpInside];
            return view;
        } else if (indexPath.section == 3) {
            ZYXHomeTwoHeaderView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"twoHeader" forIndexPath:indexPath];
            view.imgView.image = [UIImage imageNamed:@"jingpintuijian-1"];
            view.moreBtn.tag = 1001;
               [view.moreBtn addTarget:self action:@selector(clickMoreHigh:) forControlEvents:UIControlEventTouchUpInside];
            view.titleLabel.text = @"精品推荐";
            return view;
        }else if (indexPath.section == 4) {
          ZYXHomeTwoHeaderView  *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"threeHeader" forIndexPath:indexPath];
            [view.moreBtn addTarget:self action:@selector(clickMoreStudy) forControlEvents:UIControlEventTouchUpInside];
            return view;
        }else{
            UICollectionReusableView *view = [UICollectionReusableView new];
            return view;
        }
    }else{
        ZYXHomeTwoFooter *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"twoFooter" forIndexPath:indexPath];
        return view;
    }

    
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    if (section >=2) {
        return CGSizeMake(SCREEN_WIDTH, HEIGHT(40));
    }else{
        return CGSizeMake(0, 0);
    }
    
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    if (section>=1) {
         return CGSizeMake(SCREEN_WIDTH, HEIGHT(5));
    }else{
        return CGSizeMake(0, 0);
    }
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([UID integerValue] >0) {
        if (indexPath.section == 1) {
            ZYXHomeComModel *model = self.allModel.fenlei[indexPath.row];
            if ([model.title isEqualToString:@"全部"]) {
                self.tabBarController.selectedIndex = 1;
            }else{
                
                ZYXClassVC *vc = [[ZYXClassVC alloc]init];
                vc.title = model.title;
                vc.mid = model.uid;
                [self.navigationController pushViewController:vc animated:YES];
            }
        }else if (indexPath.section == 4) {
            ZYXHomeComModel *model = self.allModel.news[indexPath.row];
            
            ZYXStudyDetailVC *vc = [[ZYXStudyDetailVC alloc]init];
            vc.uid = model.uid;
            [self.navigationController pushViewController:vc animated:YES];
            
        }else{
            ZYXHomeComModel *model  = [[ZYXHomeComModel alloc]init];
            if (indexPath.section == 2) {
                model = self.allModel.xin[indexPath.row];
            }else if (indexPath.section == 3) {
                model = self.allModel.tuijian[indexPath.row];
            }
            ZYXVideoPlayVC *vc = [[ZYXVideoPlayVC alloc]init];
            vc.title = model.title;
            vc.zid = model.uid;
            [self.navigationController pushViewController:vc animated:YES];
        }
    }else{
        [ZYXLoginViewController setupLogin];
    }
    
   
    
}
#pragma mark 点击更多
-(void)clickMoreHigh:(UIButton *)sender{
  
    ZYXHighQualityVC *vc = [[ZYXHighQualityVC alloc]init];

    if (sender.tag ==1000) {
        vc.title = @"最新上架";
        vc.typeStr = @"1";
    }else{
        vc.title = @"精品推荐";
         vc.typeStr = @"2";
    }
    [self.navigationController pushViewController:vc animated:YES];
    
}
-(void)clickMoreStudy{
 
    ZYXStudyVC *vc = [[ZYXStudyVC alloc]init];
    vc.title = @"学习头条";
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark ZYXHomeHeaderCellDelegate
-(void)clickZYXHomeHeaderPic:(NSInteger)index{
    
    if ([UID integerValue] ==0) {
        [ZYXLoginViewController setupLogin];
        return;
    }
    
    NSLog(@"点击了图片");
    ZYXHomeBannerModel *model = self.allModel.banner_list[index];
    if ([model.skip_type isEqualToString:@"goods"]) {
        ///进入详细的课程
        ZYXVideoPlayVC *vc = [[ZYXVideoPlayVC alloc]init];
        vc.title = model.title;
        vc.zid = model.linkUrl;
        [self.navigationController pushViewController:vc animated:YES];
    }else{
        ///跳转到外部链接
        WebVC *vc = [[WebVC alloc]init];
        vc.isNeedShowTitle = YES;
        vc.topTitle = model.title;
        vc.urlString = model.linkUrl;
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    
    
    
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
 
}
#pragma mark 懒加载
-(NSMutableArray *)oneArr{
    if (!_oneArr) {
        _oneArr = [NSMutableArray array];
         ZYXHomeOneModel *model0= [[ZYXHomeOneModel alloc]initWitImgStr:@"dainnao" title:@"电脑办公"];
        ZYXHomeOneModel *model1= [[ZYXHomeOneModel alloc]initWitImgStr:@"kuiaji" title:@"会计"];
        ZYXHomeOneModel *model2= [[ZYXHomeOneModel alloc]initWitImgStr:@"jinrong" title:@"金融"];
        ZYXHomeOneModel *model3= [[ZYXHomeOneModel alloc]initWitImgStr:@"yingxiaoxue" title:@"营销学"];
        ZYXHomeOneModel *model4= [[ZYXHomeOneModel alloc]initWitImgStr:@"jianzhuxue" title:@"建筑学"];
        ZYXHomeOneModel *model5= [[ZYXHomeOneModel alloc]initWitImgStr:@"yixue" title:@"医学"];
        ZYXHomeOneModel *model6= [[ZYXHomeOneModel alloc]initWitImgStr:@"jingjixue" title:@"经济学"];
        ZYXHomeOneModel *model7= [[ZYXHomeOneModel alloc]initWitImgStr:@"quanbu" title:@"全部"];
        _oneArr = [NSMutableArray arrayWithObjects:model0,model1,model2,model3,model4,model5,model6,model7, nil];
       
    }
     return _oneArr;
}
-(NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}
@end
